layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  var tradeNo = $('input[name="tradeNo"]').val();
  var createDate = $('input[name="createDate"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/member/order/profit/user/list.data'
      ,where:{tradeNo:tradeNo,createDate:createDate}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
        {field: 'userId',title: '用户ID'}
      ,{field: 'name',title: '用户姓名'}
      ,{field: 'roleTypeName',title: '用户角色'}
      ,{field: 'moneyStr',title: '分成金额'}
      ,{field: 'incomeRatio', title: '分成比例',templet:function (record){
          return record.incomeRatio + '%';
        }}
      ,{field: 'createDate',title: '创建时间'}
    ]]
    ,page: false
    ,height: 'full'
    ,text: '对不起，加载出现异常！'
  });
  
  //事件
  var active = {
    divideProfit:function (){
      layer.confirm('确定要重新分成吗？', function(index){
        var loadIndex = layer.load();
        $.ajax({
          type: "post",
          url: layui.setter.webRoot + '/member/order/profit/divide.do',
          data:{tradeNo:tradeNo,createDate:createDate},
          dataType:"json",
          success:function(res){
            layer.close(loadIndex);
            if(res.code == 200){
              layer.msg('重新分成成功',{icon:1},function (){
                location.reload();
              });
            }else{
              layer.msg(res.msg,{icon: 5});
            }
          }
        });
        layer.close(index);
      });
    }
    ,revokeProfit:function (){
      layer.confirm('确定要回滚分成吗？', function(index){
        var loadIndex = layer.load();
        $.ajax({
          type: "post",
          url: layui.setter.webRoot + '/member/order/profit/revoke.do',
          data:{tradeNo:tradeNo,createDate:createDate},
          dataType:"json",
          success:function(res){
            layer.close(loadIndex);
            if(res.code == 200){
              layer.msg('回滚分成成功',{icon:1},function (){
                location.reload();
              });
            }else{
              layer.msg(res.msg,{icon: 5});
            }
          }
        });
        layer.close(index);
      });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('member_order_business_profit_info', {})
});